package com.dc.wifi.main.ui.dialog

import android.content.Context
import android.graphics.Point
import android.graphics.drawable.GradientDrawable
import android.net.wifi.WifiInfo
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import androidx.appcompat.app.AppCompatDialog
import android.widget.Button
import android.widget.TextView
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.annotation.RequiresApi
import com.dc.wifi.R

class CustomDialog(context: Context, themeResId: Int) : AppCompatDialog(context, themeResId) {
private lateinit var wifiInfo: WifiInfo
private lateinit var tv_wifi_info:TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 使用你提供的布局
        setContentView(R.layout.dialog_pop)
        tv_wifi_info= findViewById<TextView>(R.id.tv_wifi_info)!!
        window?.setLayout(
            dpToPx(638f, context),
            dpToPx(943f, context)
        )
//        window?.attributes?.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL

        val params = window?.attributes
        params?.apply {
            gravity = Gravity.CENTER  // Center horizontally
            y = dpToPx(-138f, context)          // Vertical offset of 100dp
        }
        window?.attributes = params
        findViewById<Button>(R.id.btn_close)?.setOnClickListener {
            dismiss()
        }

    }
    fun loadData(info: WifiInfo): Unit {
        wifiInfo=info
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    override fun show() {
        super.show()
        //<string name="wifi_info">SSID:\tDSTC_2.4G\nBSSID:\tD4:31:27:6E:5D:8E\n类型:\t802.11n(Wi-Fi4)\n强度:\t-45dBm\n频率:\t2412 Mhz\n信道宽度:\t20Mhz\n安全：\tWPA2/WPS</string>
//        tv_wifi_info.text=
        val stringBuilder = StringBuilder()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            stringBuilder.append("SSID:").append(wifiInfo.ssid).append("\n")
                .append("BSSID:").append(wifiInfo.bssid).append("\n")
                .append("类型:").append(wifiInfo.wifiStandard).append("G").append("\n")
                .append("强度:").append(wifiInfo.rssi).append("dBm\n")
                .append("频率:").append(wifiInfo.frequency).append(" Mhz\n")
                .append("信道宽度:").append(wifiInfo.txLinkSpeedMbps).append("Mhz\n")
                .append("安全:").append(wifiInfo.supplicantState)
        }
        tv_wifi_info.text=stringBuilder
    }
    fun dpToPx(dp: Float, context: Context): Int {
        val metrics = context.resources.displayMetrics
        return (dp * (metrics.densityDpi / 160f)).toInt() // 160f 是基线的密度值
    }
}